package com.weita.lightfood.service;


import com.weita.lightfood.model.dao.UserDao;
import com.weita.lightfood.model.entity.User;
import com.weita.lightfood.model.query.user.RegisterUserQuery;
import com.weita.lightfood.model.query.user.ResetPasswordQuery;
import com.weita.lightfood.model.vo.user.UserHistoryWeight;
import com.weita.lightfood.model.vo.user.UserWeightVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author IT_Monster at 2019/03/27 14:56.
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private HealthReportService healthReportService;

    /**
     * 通过id获取单个用户
     * @param id
     * @return
     * @throws Exception
     */
    public User getByID(int id) throws Exception {
        return userDao.getByID(id);
    }

    /**
     * 获取所有用户
     * @return
     * @throws Exception
     */
    public List<User> getAll() throws Exception {
        return userDao.getAll();
    }

    /**
     * 修改用户信息(不包括密码)  同时生成健康报表
     * @param user
     * @return
     * @throws Exception
     */
    public String update(User user) throws Exception {
        User u = userDao.getByUserName(user.getName());
        System.out.println(user);
        System.out.println(u);
        //通过新设置的用户名得到的对象是空的 或者 得到的对象就是需要更新的用户时允许更新用户信息
        if (u == null || u.getId().equals(user.getId())) {
            userDao.update(user);
            if (healthReportService.getHealthReport(user.getId()) == null) {
                //如果用户没有健康表则生成健康表
                healthReportService.creatReport(user.getId());
            } else {
                //否则更新用户健康表
                healthReportService.updateReport(healthReportService.makeReport(user.getId()));
            }
            return "修改用户信息成功";
        } else {
            return "此用户名已存在！";
        }
    }

    /**
     * 通过id删除用户
     * @param id
     * @return
     * @throws Exception
     */
    public String delete(int id) throws Exception {
        userDao.delete(id);
        return "删除了第" + id + "位用户";
    }

    /**
     * 修改用户密码
     * @param userId
     * @param oldPwd
     * @param newPwd
     * @param confirmPwd
     * @return
     * @throws Exception
     */
    public String updatePwd(int userId, String oldPwd, String newPwd, String confirmPwd) throws Exception {
        if (!confirmPwd.equals(newPwd)) {
            return "密码不一致";
        } else if (userDao.getPwdById(userId).equals(oldPwd)) {
            userDao.updatePwd(userId, newPwd);
            return "修改用户密码成功";
        } else {
            return "密码错误！";
        }
    }

    /**
     * 用户登陆验证
     * @param userName
     * @param pwd
     * @return
     * @throws Exception
     */
    public User login(String userName, String pwd, String formId) throws Exception {
        userDao.saveFormId(formId, userName);
        return userDao.login(userName, pwd);
    }

    /**
     * 注册用户
     * @param registerUserQuery
     * @return
     * @throws Exception
     */
    public String registerUser(RegisterUserQuery registerUserQuery) throws Exception {
        User user1 = userDao.getByUserName(registerUserQuery.getName());
        if (user1 == null) {
            userDao.registerUser(registerUserQuery);
            return "注册成功";
        } else {
            return "此用户名已存在";
        }
    }

    /**
     * 获取用户密保问题
     * @param userName
     * @return
     * @throws Exception
     */
    public String getSecurityQuestion(String userName) throws Exception {
        return userDao.getSecurityQuestion(userName);
    }

    /**
     * 重置用户密码
     * @param resetPasswordQuery
     * @return
     * @throws Exception
     */
    public String resetPassword(ResetPasswordQuery resetPasswordQuery) throws Exception {
        //1判断密保问题是否正确
        String answer = userDao.getSecurityAnswer(resetPasswordQuery.getUserName());
        //2如果正确，重置用户密码并返回重置成功消息
        if (resetPasswordQuery.getAnswer().equals(answer)) {
            userDao.updatePwdByName(resetPasswordQuery.getUserName(), resetPasswordQuery.getNewPwd());
            return "重置密码成功！";
        } else {
            //3如果不正确，返回答案错误消息
            return "答案错误！";
        }


    }

    /**
     * 获取用户初始体重，目标体重，最新体重
     * @param userId
     * @return
     * @throws Exception
     */
    public UserWeightVo getWeight(int userId) throws Exception {
        User user = userDao.getByID(userId);
        UserWeightVo weightVo = new UserWeightVo();
        if (user != null) {
            //调整数字格式
            double weightWeekly = (user.getFirstWeight() - user.getAimWeight()) / user.getWeightTime();
            weightWeekly = (1.0 * ((int) (weightWeekly * 100))) / 100;

            //只获取日期年月日
            String aimTime = user.getAimTime();
            aimTime = aimTime.substring(0, 10);
            String beginTime = user.getBeginTime();
            beginTime = beginTime.substring(0, 10);

            weightVo.setAimTime(aimTime);
            weightVo.setAimWeight(user.getAimWeight());
            weightVo.setLoseWeightWeekly(weightWeekly);
            weightVo.setBeginTime(beginTime);
            weightVo.setFirstWeight(user.getFirstWeight());
            weightVo.setNewWeight(user.getNewWeight());
        }
        return weightVo;
    }

    /**
     * 设置用户最新体重
     * @param userId          用户id
     * @param newWeight         最新体重
     * @param recordDate        记录日期
     * @return
     * @throws Exception
     */
    public String recordWeight(int userId, double newWeight, String recordDate) throws Exception {
        userDao.recordWeight(userId, newWeight);
        userDao.recordHistoryWeight(userId, newWeight, recordDate);
        return "记录新体重成功";
    }





    /**
     * 保存用户openId
     * @param formId
     * @param name
     */
    public void saveFormId(String formId, String name) throws Exception {
        userDao.saveFormId(formId, name);
    }

    /**
     * 获取用户openId
     * @param userId
     * @return
     */
    public String getFormId(int userId) throws Exception {
        return userDao.getFormId(userId);
    }

    /**
     * 获取用户历史体重
     * @param userId
     * @return
     * @throws Exception
     */
    public List<UserHistoryWeight> getUserHistoryWeight(int userId) throws Exception{
        return userDao.getHistoryWeight(userId);
    }

    /**
     * 获取用户openId
     * @param userId
     * @return
     * @throws IOException
     */
    public String getOpenId(int userId) throws IOException {
        return userDao.getOpenId(userId);
    }

    /**
     * 设置或更新用户openid
     * @param userId
     * @param newOpenId
     * @throws IOException
     */
    public void setOrUpdateOpenId(int userId, String newOpenId) throws IOException {
        String openId = this.getOpenId(userId);
        if ( newOpenId.equals(openId) ){
            return;
        }else {
            userDao.setOpenId(userId, newOpenId);
        }
    }

}
